# -*- coding: utf-8 -*-
"""
Web framework/Tornado Escape/test02.py
"""
import tornado.web
import tornado.escape
import asyncio


class Utf8Handler(tornado.web.RequestHandler):
    """
    tornado.escape.utf8
    """

    def get(self):
        p_args = self.get_argument("char")
        p_convert_args = tornado.escape.utf8(p_args)
        self.finish(str(p_convert_args))
    # > curl http://127.0.0.1:8888/utf8?char=%E4%BD%A0%E5%A5%BDABC
    # > HTTP Response: b'\xe4\xbd\xa0\xe5\xa5\xbdABC'


class ToUnicodeHandler(tornado.web.RequestHandler):
    """
    tornado.escape.to_unicode
    """

    def get(self):
        p_args = self.get_argument("char")
        p_convert_args = tornado.escape.utf8(p_args)
        self.write(str(type(p_convert_args)))
        p_convert_args = tornado.escape.to_unicode(p_args)
        self.write(str(type(p_convert_args)))
        self.finish()
    # > curl http://127.0.0.1:8888/utf8?char=%E4%BD%A0%E5%A5%BDABC
    # > HTTP Response: <class 'bytes'><class 'str'>


class ToBaseStringHandler(tornado.web.RequestHandler):
    """
    tornado.escape.to_basestring
    """

    def get(self):
        p_args = self.get_argument("char")
        p_convert_args = tornado.escape.to_basestring(bytes(p_args, encoding="utf8"))
        self.finish(p_convert_args)
    # > curl http://127.0.0.1:8888/to_basestring?char=%E4%BD%A0%E5%A5%BDABC
    # > HTTP Response: 你好ABC


class RecursiveUnicodeHandler(tornado.web.RequestHandler):
    """
    tornado.escape.recursive_unicode
    """

    def get(self):
        p_args = self.get_argument("char")
        o_res = {
            "param": p_args
        }
        p_convert_args = tornado.escape.recursive_unicode(o_res)
        self.finish(p_convert_args)
    # > curl http://127.0.0.1:8888/to_basestring?char=%E4%BD%A0%E5%A5%BDABC
    # > HTTP Response: {
    #     "param": "你好ABC"
    # }


class LinkifyHandler(tornado.web.RequestHandler):
    """
    tornado.escape.linkify
    """

    def get(self):
        p_args = self.get_argument("char")
        p_convert_args = tornado.escape.linkify(f"{p_args} http://127.0.0.1:8888/")
        self.finish(p_convert_args)
    # > curl http://127.0.0.1:8888/linkify?char=%E4%BD%A0%E5%A5%BDABC
    # > HTTP Response: 你好ABC <a href="http://127.0.0.1:8888/">http://127.0.0.1:8888/</a>


class SqueezeHandler(tornado.web.RequestHandler):
    """
    tornado.escape.squeeze
    """

    def get(self):
        p_args = self.get_argument("char")
        p_convert_args = tornado.escape.squeeze(p_args)
        self.finish(p_convert_args)
    # > curl http://127.0.0.1:8888/squeeze?char=%E4%BD%A0%20%20%20%E5%A5%BD%20%20%20%20A%20%20%20%20%20B%20%20%20%20C
    # > HTTP Response: 你 好 A B C


async def main():
    app = tornado.web.Application([
        (r'/utf8', Utf8Handler),
        (r'/to_unicode', ToUnicodeHandler),
        (r'/to_basestring', ToBaseStringHandler),
        (r'/recursive_unicode', RecursiveUnicodeHandler),
        (r'/linkify', LinkifyHandler),
        (r'/squeeze', SqueezeHandler),
    ])
    app.listen(8888)
    await asyncio.Event().wait()


if __name__ == '__main__':
    asyncio.run(main())
